package com.youtellv1.mapper;

import com.youtellv1.dto.YSalDTO;
import com.youtellv1.entity.YSal;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface YSalMapper extends BaseMapper<YSal>{
    @Select("select count(*) from tb_ysal where tid = #{tid}")
    int countById(@Param("tid") Integer tid);

    @Select("select * from tb_ysal where sid = #{sid}")
    YSal findById(@Param("sid") Integer sid);

    @Select("SELECT y.*, t.tname, t.tsex, t.tage, t.tphone " +
            "FROM tb_ysal y " +
            "LEFT JOIN tb_teachers t ON y.tid = t.tid " +
            "WHERE y.tid = #{tid} " +
            "ORDER BY y.month DESC " +
            "LIMIT #{offset}, #{size}")
    List<YSalDTO> findByTidWithPage(@Param("tid") Integer tid,
                                    @Param("offset") int offset,
                                    @Param("size") int size);

    @Insert("insert into tb_ysal(base_sal, subsidy, class_size, health, year, edu, merits_sal1, merits_sal2, merits_sal3, merits_sal4, merits_sal5, full_time, safe, class_hour, newstu, for_leave, other, class_out, add_class, class_num, class_reg, sign_num, month, tid) values (#{base_sal}, #{subsidy}, #{class_size}, #{health}, #{year}, #{edu}, #{merits_sal1}, #{merits_sal2}, #{merits_sal3}, #{merits_sal4}, #{merits_sal5}, #{full_time}, #{safe}, #{class_hour}, #{newstu}, #{for_leave}, #{other}, #{class_out}, #{add_class}, #{class_num}, #{class_reg}, #{sign_num}, #{month}, #{tid})")
    void insert(YSalDTO ysaldto);

    @Update("<script>" +
            "update tb_ysal " +
            "<set>" +
            "    <if test='base_sal != null'>base_sal = #{base_sal},</if>" +
            "    <if test='subsidy != null'>subsidy = #{subsidy},</if>" +
            "    <if test='class_size != null'>class_size = #{class_size},</if>" +
            "    <if test='health != null'>health = #{health},</if>" +
            "    <if test='year != null'>year = #{year},</if>" +
            "    <if test='edu != null'>edu = #{edu},</if>" +
            "    <if test='merits_sal1 != null'>merits_sal1 = #{merits_sal1},</if>" +
            "    <if test='merits_sal2 != null'>merits_sal2 = #{merits_sal2},</if>" +
            "    <if test='merits_sal3 != null'>merits_sal3 = #{merits_sal3},</if>" +
            "    <if test='merits_sal4 != null'>merits_sal4 = #{merits_sal4},</if>" +
            "    <if test='merits_sal5 != null'>merits_sal5 = #{merits_sal5},</if>" +
            "    <if test='full_time != null'>full_time = #{full_time},</if>" +
            "    <if test='safe != null'>safe = #{safe},</if>" +
            "    <if test='class_hour != null'>class_hour = #{class_hour},</if>" +
            "    <if test='newstu != null'>newstu = #{newstu},</if>" +
            "    <if test='for_leave != null'>for_leave = #{for_leave},</if>" +
            "    <if test='other != null'>other = #{other},</if>" +
            "    <if test='class_out != null'>class_out = #{class_out},</if>" +
            "    <if test='add_class != null'>add_class = #{add_class},</if>" +
            "    <if test='class_num != null'>class_num = #{class_num},</if>" +
            "    <if test='class_reg != null'>class_reg = #{class_reg},</if>" +
            "    <if test='sign_num != null'>sign_num = #{sign_num},</if>" +
            "    <if test='month != null'>month = #{month},</if>" +
            "    <if test='tid != null'>tid = #{tid}</if>" +
            "</set> " +
            "where sid = #{sid}" +
            "</script>")
    void update(YSalDTO ysaldto);

    @Delete("delete from tb_ysal where sid = #{sid}")
    void delete(@Param("sid") Integer sid);
}
